package com.cimr.controller.manager;

import com.cimr.comm.dto.HttpResult;
import com.cimr.comm.dto.LayuiTableData;
import com.cimr.dto.SelectDto;
import com.cimr.po.manager.Area;
import com.cimr.service.manager.AreaService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by suhuanzhao on 2018/1/3.
 */
@Controller
@RequestMapping("/area/ajax")
public class AreaAjaxController {

    @Autowired
    private AreaService areaService;


    /**
     * 列表查询
     *
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping({"getList"})
    @ResponseBody
    public LayuiTableData list(@RequestParam(required = true, defaultValue = "1") Integer page,
                               @RequestParam(required = false, defaultValue = "10") Integer limit,
                               @RequestParam(required = false) String title) {
        PageHelper.startPage(page, limit);

        Area area = new Area();
        area.setTitle(title);
        List<Area> list = areaService.selectList_commom(area);
        PageInfo<Area> pageInfo = new PageInfo(list);
        LayuiTableData result = new LayuiTableData();
        result.setCode(0);
        result.setMsg("");
        result.setCount((int) pageInfo.getTotal());
        result.setData(list);
        return result;
    }

    /**
     * 保存和更新
     *
     * @param session
     * @return
     */
    @RequestMapping({"save"})
    @ResponseBody
    public HttpResult save(Area area, HttpSession session) {

        HttpResult res = new HttpResult(true, "添加成功");
        try {
            if (area.getId() == null || "".equals(area.getId())) {
                //添加操作
                areaService.insert(area.getPid(), area.getAreaId(), area.getTitle(), area.getCode(), area.getCountryCode(), area.getOrderId());
            } else {
                //更新操作
                areaService.update(area.getId(), area.getPid(), area.getAreaId(), area.getTitle(), area.getCode(), area.getCountryCode(), area.getOrderId());
            }
        } catch (Exception e) {
            res.setSuccess(false);
            res.setError("操作失败，系统出现异常！");
            e.printStackTrace();
        }

        return res;
    }

    //删除
    @RequestMapping({"delete"})
    @ResponseBody
    public HttpResult delete(String areaId) {
        HttpResult res = new HttpResult(true, "删除成功");
        try {
            areaService.deleteById_common(areaId);
        } catch (Exception e) {
            res.setSuccess(false);
            res.setError("操作失败，系统出现异常！");
            e.printStackTrace();
        }
        return res;
    }


    //查询单个userInfo数据
    @RequestMapping({"getAreaInfo"})
    @ResponseBody
    public HttpResult edit(@RequestParam(required = true) String areaId) {
        HttpResult res = new HttpResult(true, "获取成功");
        try{
            Area area = areaService.selectById_common(areaId);
            res.setData(area);
        }catch (Exception e){
            res.setSuccess(false);
            res.setError("操作失败，系统出现异常！");
            e.printStackTrace();
        }
        return res;
    }

    @ResponseBody
    @RequestMapping("/getAreaData")
    public HttpResult getSelectData(@RequestParam("pid") String pid) {
        List<SelectDto<String, String>> selectDtoList = new ArrayList<>();
        Area area = new Area();
        area.setPid(pid);
        List<Area> list = areaService.selectList_commom(area);
        for (Area a : list) {
            selectDtoList.add(new SelectDto<String, String>(a.getTitle(), a.getAreaId()));
        }
        return new HttpResult(true, selectDtoList);
    }

}
